import config from "@/settings";
import { regUrl } from "@/utils/validate";

/**
 * 根据路由地址获取子应用名称（获取第一个/前的字符串）
 * @param path
 * @return {string}
 */
export const getSubstringBeforeSecondSlash = path => {
  if (!path || typeof path !== "string") {
    return "";
  }
  const parts = path.split("/");
  if (parts.length > 2) {
    return parts[1];
  } else {
    return "";
  }
};

/**
 * @description 获取url中的参数
 * @param name -字段名
 * @returns {string}
 */
export const getUrlKey = name => {
  // const corpId = /corpId=([^=&]*)/.exec(window.location.href)[1]
  let url = window.location.href,
    urlInfo = url.split("?")[1],
    searchParams = new URLSearchParams("?" + urlInfo);
  return searchParams.get(name);
};

/**
 * @description 获取url
 * @param url
 * @return {*}
 */
export const delSearchParams = url => {
  if (regUrl(url)) {
    return url.includes("?") ? url.split("?")[0] : url;
  }
  return "";
};

/**
 * @description 钉钉免登url
 * @param clientId
 * @param corpId
 */
export const ddFreeLoginUrl = (clientId, corpId) => {
  return `https://login.dingtalk.com/oauth2/auth?redirect_uri=${config.dd_redirect_uri}&corpId=${corpId}&org_type=management&response_type=code&client_id=${clientId}&scope=openid corpid&state=pc&prompt=consent`;
};

/**
 * @description sso_login单点登录url
 */
export const ssoLoginUrl = () => {
  return `https://login.dingtalk.com/oauth2/auth?redirect=${config.sso_redirect_uri}&mode=${config.sso_mode}`;
};
